<template>
    <div class="">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
            :leave-active-class="proxy?.animate.searchAnimate.leave">
            <div>
                <searchrevealing>
                    <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
                        <el-form-item label="招商部" prop="recruiterDept">
                            <el-select style="width: 220px" v-model="queryParams.recruiterDept" placeholder="请选择招商部"
                                clearable @change="deptChange">
                                <el-option v-for="dict in deptList" :key="dict.deptId" :label="dict.deptName"
                                    :value="dict.deptId" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="招商员" prop="recruiter">
                            <el-select style="width: 220px" v-model="queryParams.recruiter" placeholder="请选择招商员"
                                clearable>
                                <el-option v-for="dict in recruiterList" :key="dict.userId" :label="dict.nickName"
                                    :value="dict.userId" />
                            </el-select>
                        </el-form-item>
                        <el-form-item label="选品日期" prop="date">
                            <el-date-picker v-model="queryParams.date" type="date" placeholder="请选择选品日期" clearable
                                style="width: 220px" />
                        </el-form-item>
                        <el-form-item label="样品状态" prop="status">
                            <el-select v-model="queryParams.status" placeholder="请选择样品状态" clearable
                                style="width: 220px">
                                <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleQuery">查询</el-button>
                            <el-button @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </searchrevealing>
                <el-card shadow="never">
                    <template #header>
                        <el-row :gutter="10" class="mb8">
                            <el-col :span="1.5">
                                <el-button type="primary" :disabled="multiple" @click="">出库</el-button>
                            </el-col>
                            <el-col :span="1.5">
                                <el-button type="primary" :disabled="multiple" @click="">导出</el-button>
                            </el-col>
                            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                        </el-row>
                    </template>
                    <el-table v-loading="loading" :data="selectionRecordList" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55" align="center" />
                        <el-table-column label="序号" type="index" width="55" align="center" />
                        <el-table-column label="商品名称" align="center" prop="styleName" />
                        <el-table-column label="商品编码" align="center" prop="styleCode" />
                        <el-table-column label="商品主图" align="center" prop="productImage" width="100">
                            <template #default="scope">
                                <image-preview :src="scope.row.productImage" :width="50" :height="50" />
                            </template>
                        </el-table-column>
                        <el-table-column label="价格" align="center" prop="price" />
                        <el-table-column label="机制" align="center" prop="liveStreamingMechanism" />
                        <el-table-column label="佣金" align="center" prop="commission" />
                        <el-table-column label="类目" align="center" prop="categoryName" />
                        <el-table-column label="品牌" align="center" prop="brand">
                            <template #default="scope">
                                {{ handleFilterBrand(scope.row.brand, brandList) }}
                            </template>
                        </el-table-column>
                        <el-table-column label="店铺名称" align="center" prop="storeName" />
                        <el-table-column label="招商员" align="center" prop="recruiter" />
                        <el-table-column label="选品部门" align="center" prop="choosePerson" />
                        <el-table-column label="选品意见" align="center" prop="chooseView" />
                        <el-table-column label="商品卖点" align="center" prop="sellingPoints" />
                        <el-table-column label="状态" align="center" prop="auditStatus">
                            <template #default="scope">
                                <div>{{ auditStatusNameById(scope.row.auditStatus) }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template #default="scope">
                                <el-button link type="primary" @click="handleInfo(scope.row)">
                                    详情</el-button>
                                <el-button link type="primary" @click="modify(scope.row)"
                                    style="margin-left: 0px !important">
                                    修改</el-button>
                                <el-button link type="primary" @click="" style="margin-left: 0px !important">
                                    出库</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize" @pagination="getList" />
                </el-card>
            </div>

        </transition>

        <!-- 商品修改 -->
        <commoditymodify @doClose="commoditymodifyClose" :commoditymodifyData="commoditymodifyData"
            :commoditymodifyVisible="commoditymodifyVisible" :type="'anchor'"></commoditymodify>
        <!--商品详情抽屉 -->
        <commoditydetails @doClose="commoditydetailsClose" :commoditydetailsData="commoditydetailsData"
            :dialogInfoVisible="dialogInfoVisible"  :type="'anchor'"></commoditydetails>
    </div>
</template>

<script setup name="SelectionRecord" lang="ts">
import commoditydetails from '../../components/commoditydetails.vue'
import { SelectionRecordQueryParams, SelectionRecordVO } from '@/api/group/investmentPromotion/types'
import commoditymodify from '../../components/commoditymodify.vue'
import { listBrand } from '@/api/system/brand';
import { BrandVO } from '@/api/system/brand/types';
import { auditStatusNameById } from '@/utils/auditStatusFilter'
import { getDeptList, getUserById } from '@/api/group/qualityControl'
import { QualityControlForm, QualityControlQuery } from '@/api/group/qualityControl/types';
import { SupplierAddProductsVO } from '@/api/system/supplierAddProducts/types';
import { listSupplierAddProduct, getSupplierAddProducts, getproductscategorychildlistbypid } from '@/api/system/supplierAddProducts';

const queryFormRef = ref<ElFormInstance>();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const deptList = ref<{ deptId: string; deptName: string; }[]>([])
const recruiterList = ref<{ userId: string; nickName: string; }[]>([])
const showSearch = ref(true);
const loading = ref(true);
const total = ref(0);
const brandList = ref<BrandVO[]>([]);
const ids = ref<Array<string | number>>([]);
const multiple = ref(true);
const selectionRecordList = ref<SelectionRecordVO[]>([]);

const commoditymodifyVisible = ref(false)
const commoditymodifyData = ref<QualityControlForm>({})

const dialogInfoVisible = ref(false)
const commoditydetailsData = ref<QualityControlForm>({})

const queryParams = ref<SelectionRecordQueryParams>({
    pageNum: 1,
    pageSize: 10,
    dept: undefined,
    recruiter: undefined,
    date: undefined,
    status: undefined,
})

const statusList = reactive([
    { label: '测试样品状态: 已上架', value: '1' },
    { label: '测试样品状态：未上架', value: '2' },
])

/** 修改 */
const modify = async (row?: SupplierAddProductsVO) => {
    const res: any = await getSupplierAddProducts((row as any).id);
    commoditymodifyData.value = res.data
    commoditymodifyVisible.value = true
};
/**
 * 修改关闭
 */
const commoditymodifyClose = () => {
    commoditymodifyVisible.value = false
    getList();
};
/** 查看商品详情 */
const handleInfo = (row?: SupplierAddProductsVO) => {
    nextTick(async () => {
        const _id = row?.id || ids.value[0];
        const res: any = await getSupplierAddProducts(_id);
        dialogInfoVisible.value = true;
        commoditydetailsData.value = res.data
    });
};

/** 商品详情关闭 */
const commoditydetailsClose = () => {
    dialogInfoVisible.value = false;
}

/**
 * 获取招商部门信息
 */
const getDeptLists = async () => {
    const res = await getDeptList({ parentId: '1782395704411734018' })
    deptList.value = res.data
}

/**
 *  根据招商部获取招商员
 */
const deptChange = async (event: string) => {
    if (event) {
        const res = await getUserById(event)
        recruiterList.value = res.data
        queryParams.value.recruiter = undefined
    } else {
        recruiterList.value = []
        queryParams.value.recruiter = undefined
    }
}
/**
 * 获取品牌信息
 */
const getListBrand = async () => {
    const res = await listBrand()
    brandList.value = res.rows;
}
/**
 * 品牌检索value
 * @param para 
 * @param dict 
 */
const handleFilterBrand = (para: string | number | undefined, dict: Array<any>) => {
    let _label = '';
    dict.forEach((item: any) => {
        if (item.id == String(para)) {
            _label = item.brandName;
        }
    });
    return _label;
};
/** 查询列表 */
const getList = async () => {
    try {
        loading.value = true;
        const res = await listSupplierAddProduct(queryParams.value);
        selectionRecordList.value = res.rows;
        total.value = res.total;
        loading.value = false;
    } catch (e) {
    } finally {
        loading.value = false;
    }
};
/** 多选框选中数据 */
const handleSelectionChange = (selection: SelectionRecordVO[]) => {
    ids.value = selection.map((item) => item.id);
    multiple.value = !selection.length;
};
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
    queryFormRef.value?.resetFields();
    handleQuery();
};
onMounted(() => {
    queryParams.value.pageNum = 1;
    getDeptLists();
    getListBrand();
    getList();
})

</script>
<style lang="scss" scoped></style>
